package com.gimi.cloud.bbp.client.service.moreInfo.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gimi.cloud.bbp.client.service.moreInfo.CreditcardBankcodeService;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.commonService.InterfacePlatform.InterfacePlatformRequestService;
import com.gimi.cloud.bbp.dao.dao.moreInfo.CreditcardBankcodeDao;
import com.gimi.cloud.bbp.dao.dao.moreInfo.MoreInfoDao;
import com.gimi.cloud.bbp.dao.dao.sys.SysConfigDao;
import com.gimi.cloud.bbp.dto.app.moreInfo.CreditcardBankcodeDTO;
import com.gimi.cloud.bbp.dto.app.moreInfo.CreditcardLoginDTO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @auther loongs
 * @date 2018/1/12  13:30
 */
@Service
public class CreditcardBankcodeServiceImpl implements CreditcardBankcodeService {
    private static final Logger log = LoggerFactory.getLogger(CreditcardBankcodeServiceImpl.class);
    private final static String INTERFACE_PLATFORM_UNIVERSAL_CREDITCARDWATER = "interface_platform_universal_creditCardWater";

    private CreditcardBankcodeDao creditcardBankcodeDao;
    private InterfacePlatformRequestService ipRequestService;
    private SysConfigDao sysConfigDao;
    private MoreInfoDao moreInfoDao;
   @Autowired
    public CreditcardBankcodeServiceImpl(CreditcardBankcodeDao creditcardBankcodeDao, InterfacePlatformRequestService ipRequestService,
                                         SysConfigDao sysConfigDao, MoreInfoDao moreInfoDao) {
        this.creditcardBankcodeDao = creditcardBankcodeDao;
        this.ipRequestService = ipRequestService;
        this.sysConfigDao = sysConfigDao;
        this.moreInfoDao = moreInfoDao;
    }

    @Override
    public Response getSupportedCreditcardBanks(CreditcardBankcodeDTO creditcardBankcodeDTO) {
        Map<String, Object> map = new HashMap<>();
        Response response = new Response();
        map.put("bankList", creditcardBankcodeDao.selectSupportedBanks());
        response.setRes_data(map);
        response.setResponseCode(ResponseCodeEnum.SUCCESSED);
        return response;
    }

    @Override
    public Response getCreditcardLoginParameter(CreditcardBankcodeDTO creditcardBankcodeDTO) {
        Map<String, Object> map = new HashMap<>();
        Response response = new Response();
        map.put("bankParam", creditcardBankcodeDao.selectByPrimaryKey(creditcardBankcodeDTO.getFcbcId()));
        response.setRes_data(map);
        response.setResponseCode(ResponseCodeEnum.SUCCESSED);
        return response;
    }

    @Override
    public Response subCreditcardBasicParam(CreditcardLoginDTO creditcardLoginDTO) {
        log.info("开始发起信用卡网银登录");

        log.info("读取配置表并组装请求参数");
        String fengchao_gongjijing = sysConfigDao.getConfigByKey(INTERFACE_PLATFORM_UNIVERSAL_CREDITCARDWATER);
        JSONObject fgJson = JSON.parseObject(fengchao_gongjijing);
        JSONObject submitbasicinfo = fgJson.getJSONObject("fengchao_bankonline_submitbasicinfo");
        JSONObject body = submitbasicinfo.getJSONObject("body");
        body.put("bankCode", creditcardLoginDTO.getBankCode());
        body.put("args", creditcardLoginDTO.getLoginParameter());
        submitbasicinfo.put("body", body);

        Response response = new Response();
        try {
            log.info("提交基本参数尝试登陆");
            String sendApiPost = ipRequestService.sendApiPost(submitbasicinfo.toJSONString());
            JSONObject sessionJson = JSON.parseObject(sendApiPost);
            JSONObject sessionbody = sessionJson.getJSONObject("body");
            String sessionToken = sessionbody.getString("sessionToken");

            if (!"1".equals(sessionbody.getString("status")) || StringUtils.isBlank(sessionToken)) {
                response.setResponseCode(ResponseCodeEnum.LOGIN_EXCEPTION, sessionbody.getString("errors"));
                return response;
            }

            JSONObject getloginstatus = fgJson.getJSONObject("fengchao_bankonline_getloginstatus");
            JSONObject glBody = getloginstatus.getJSONObject("body");
            glBody.put("sessionToken", sessionToken);

            log.info("保存TOKEN");
            Integer operUserId = creditcardLoginDTO.getUserId();
//            FengchaoCyberbankingReportEntity reportEntity = fengchaoCyberbankingReportMapper.queryBy("user_id",
//                    operUserId);
//            if (reportEntity == null) {
//                reportEntity = new FengchaoCyberbankingReportEntity();
//                reportEntity.setUserId(operUserId);
//                reportEntity.setFcbrSessionToken(sessionToken);
//                fengchaoCyberbankingReportMapper.baseInsert(reportEntity);
//            }

            getloginstatus.put("body", glBody);
            String status = null;
            JSONObject glbody = null;
            do {
                String glRep = ipRequestService.sendApiPost(getloginstatus.toJSONString());
                JSONObject glRepJson = JSON.parseObject(glRep);
                glbody = glRepJson.getJSONObject("body");
                status = glbody.getString("status");
            } while ("101".equals(status));

            if (status == null || "-1".equals(status)) {
                response.setResponseCode(ResponseCodeEnum.SERVICE_ERROR);
                return response;
            }

            switch (status) {
                case "100":
                    break;
                case "001":
                case "002":
                case "003":
                    response.setResponseCode(ResponseCodeEnum.NEED_VALIDATION);
                    response.setRes_data(glbody);
                    return response;
                default:
                    response.setResponseCode(ResponseCodeEnum.LOGIN_EXCEPTION, glbody.getString("errors"));
                    return response;
            }

            response.setResponseCode(ResponseCodeEnum.SUCCESSED);
            return response;
        } catch (Exception e) {
            log.info("基本参数登录异常" + e.getClass());
            e.printStackTrace();
            response.setResponseCode(ResponseCodeEnum.SERVICE_ERROR);
            return response;
        }
    }
}
